package com.tangsm.spring.boot.aliyun.bailian.model;

import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    // 成功（不带数据）
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMsg("成功");
        return result;
    }

    // 成功（带数据）
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMsg("成功");
        result.setData(data);
        return result;
    }

    // 错误（带错误信息）
    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.setCode(500);
        result.setMsg(message);
        return result;
    }

    // 错误（带错误码和信息）
    public static <T> Result<T> error(int code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(message);
        return result;
    }

    // 链式调用设置扩展信息
    public Result<T> withData(T data) {
        this.data = data;
        return this;
    }

    // 常用错误码枚举
    public enum ErrorCode {
        PARAM_ERROR(400, "参数错误"),
        SYSTEM_ERROR(500, "系统错误");

        private final int code;
        private final String desc;

        ErrorCode(int code, String desc) {
            this.code = code;
            this.desc = desc;
        }
    }
}